package com.sinodata.bsm.cicp.finder.solaris;

/**
 * Solaris内存发现程序.
 * 程序返回：内存页大小，内存大小
 */
import java.io.BufferedReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.util.CommandReader;

public class Mem extends SolarisFinder {

    private static final Logger logger = Logger.getLogger(Mem.class);
    private static String SHELL = "prtconf | grep Memory | awk '{print $3}';getconf PAGE_SIZE";

    @SuppressWarnings("unchecked")
    @Override
    public List<String[]> find(String ip, Map params) {
        List<String[]> list = new ArrayList<String[]>();
        String[] memRes = null;
        BufferedReader reader = null;
        String strLine = null;
        try {
            reader = CommandReader.getReader(ip, SHELL, 0, 0);
            List<String> strList = new ArrayList<String>();
            while ((strLine = reader.readLine()) != null) {
                strList.add(strLine.trim());
            }
            memRes = new String[5];
            list.add(memRes);
            memRes[0] = "0";
            memRes[1] = "Memory";
            memRes[2] = getHostName(ip) + "Memory";
            memRes[3] = strList.get(1); //内存页大小
            memRes[4] = strList.get(0); //内存大小
            return list;
        } catch (IOException e) {
            logger.error("failed to find solaris memory", e);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    logger.error("failed to close reader", e);
                }
            }
        }
        return null;
    }
}
